<template>
  <zy-topBar showBack title="蓝牙" paddingTop="40" height="40"> </zy-topBar>
  <view class="content">
    <button @tap="openBuletooth">openBuletooth</button>
    <button class="one" @click="searchBlue">开始搜索蓝牙</button>
    <button class="one" @click="stopBluetoothDevicesDiscovery">
      停止搜索蓝牙
    </button>
    <view>{{}}</view>
    <button class="one" @click="createBlue">连接蓝牙</button>
    <button class="one" @click="getBLEDeviceServices">获取设备特征</button>
    <button class="one" @click="writeBLECharacteristicValue">写入</button>
    <button class="one" @click="readBLECharacteristicValue">读取</button>
    <bluetooth></bluetooth>
  </view>
</template>

<script setup lang="ts">
import { ref } from "vue";
import bluetooth from "@/uni_modules/bluetooth-module/components/bluetooth-module/bluetooth-module.vue";
import { onMounted } from "vue";
onMounted(() => {
});
const list = ref([]);
//初始化蓝牙模块
const openBuletooth = () => {
  uni.openBluetoothAdapter({
    success: (res) => {
      console.log("初始化 success", res);
      //获取本机蓝牙适配器状态
      uni.getBluetoothAdapterState({
        success: function (res) {
          console.log("获取本机蓝牙适配器状态 success", res);
          if (res.available) {
            if (res.discovering) {
              stopBluetoothDevicesDiscovery();
            }
            //搜索蓝牙
            uni.startBluetoothDevicesDiscovery({
              success(res) {
                console.log("搜索蓝牙 success", res);

                uni.showToast({ title: '搜索到以下设备', icon: 'none' })
              }
            })
            that.devices = []
          } else {
            uni.showToast({ title: '本机蓝牙不可用', icon: 'none' })
          }
        },
      });
      //获取在蓝牙模块生效期间所有已发现的蓝牙设备
      uni.getBluetoothDevices({
        success: (res) => {
          console.log("获取在蓝牙模块生效期间所有已发现的蓝牙设备", res);
        },
        fail: (res) => {
          console.log("getBluetoothDevices fail", res);
        },
      });
    },
    fail: (res) => {
      console.log("openBluetoothAdapter fail", res);
      uni.showToast({
        title: "蓝牙设备未开启",
        icon: "none",
        duration: 2000,
      });
    },
  });
};
//开始搜索蓝牙
const searchBlue = () => {
  uni.startBluetoothDevicesDiscovery({
    allowDuplicatesKey: true,
    success: (res) => {
      console.log("startBluetoothDevicesDiscovery success", res);
    },
    fail: (res) => {
      console.log("startBluetoothDevicesDiscovery fail", res);
      uni.showToast({
        title: "蓝牙设备未开启",
        icon: "none",
        duration: 2000,
      });
    },
  });
};
// 停止搜索蓝牙
const stopBluetoothDevicesDiscovery = () => {
  uni.stopBluetoothDevicesDiscovery({
    success: (res) => {
      console.log("stopBluetoothDevicesDiscovery success", res);
    },
    fail: (res) => {
      console.log("stopBluetoothDevicesDiscovery fail", res);
    },
  });
};
//连接蓝牙
const getBLEDeviceServices = () => {
  uni.getBLEDeviceServices({
    deviceId: "05:68:AF:5B:4F:41",
    success(res) {
      console.log(res, "获取设备特征");
    },
    fail(res) {
      console.log(res, "获取设备特征失败");
    },
  });
};
//写入
const writeBLECharacteristicValue = () => {
  uni.writeBLECharacteristicValue({
    deviceId: "F0:0F:0C:0B:0A:09",
    serviceId: "0000ffe0-0000-1000-8000-00805f9b34fb",
    characteristicId: "0000ffe1-0000-1000-8000-00805f9b34fb",
    value: new ArrayBuffer(1),
    success(res) {
      console.log(res, "写入成功");
    },
    fail(res) {
      console.log(res, "写入失败");
    },
  });
};
//读取
const readBLECharacteristicValue = () => {
  uni.readBLECharacteristicValue({
    deviceId: "F0:0F:0C:0B:0A:09",
    serviceId: "0000ffe0-0000-1000-8000-00805f9b34fb",
    characteristicId: "0000ffe1-0000-1000-8000-00805f9b34fb",
    success(res) {
      console.log(res, "读取成功");
    },
    fail(res) {
      console.log(res, "读取失败");
    },
  });
};
const createBlue = () => {
  //连接蓝牙
  uni.createBLEConnection({
    deviceId: "F0:0F:0C:0B:0A:09",
    success(res) {
      console.log(res, "连接成功");

      //连接成功之后  停止搜索
      uni.stopBluetoothDevicesDiscovery({
        success(res) {
          console.log("停止搜索蓝牙", res);
        },
      });

      uni.getConnectedBluetoothDevices({
        //已连接
        success(res) {
          console.log(res.devices, "已连接过的设备");
        },
      });
    },
    fail(res) {
      uni.showToast({
        title: "蓝牙连接失败",
        icon: "none",
      });
      //uni.hideLoading()
    },
    computed(res) {
      //设备获取完成之后  停止搜索
      uni.stopBluetoothDevicesDiscovery({
        success(res) {
          console.log("停止搜索蓝牙", res);
        },
      });
    },
  });
};
</script>

<style>
.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
</style>
